package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppCompanyRules;
import com.ruoyi.background.mapper.AppCompanyRulesMapper;
import com.ruoyi.background.service.IAppCompanyRulesService;
import com.ruoyi.background.utils.SYS_CONSTANT;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 规章制度Service业务层处理
 *
 * @author susu
 * @date 2024-08-05
 */
@Service
public class AppCompanyRulesServiceImpl implements IAppCompanyRulesService {
    @Autowired
    private AppCompanyRulesMapper appCompanyRulesMapper;

    /**
     * 查询规章制度
     *
     * @param id 规章制度主键
     * @return 规章制度
     */
    @Override
    public AppCompanyRules selectAppCompanyRulesById(Long id) {
        return appCompanyRulesMapper.selectAppCompanyRulesById(id);
    }

    /**
     * 查询规章制度列表
     *
     * @param appCompanyRules 规章制度
     * @return 规章制度
     */
    @Override
    public List<AppCompanyRules> selectAppCompanyRulesList(AppCompanyRules appCompanyRules) {
        return appCompanyRulesMapper.selectAppCompanyRulesList(appCompanyRules);
    }

    /**
     * 新增规章制度
     *
     * @param appCompanyRules 规章制度
     * @return 结果
     */
    @Override
    public int insertAppCompanyRules(AppCompanyRules appCompanyRules) {
        appCompanyRules.setCreateTime(DateUtils.getNowDate());
        appCompanyRules.setCreateBy(SecurityUtils.getUsername());
        appCompanyRules.setUpdateTime(DateUtils.getNowDate());
        appCompanyRules.setUpdateBy(SecurityUtils.getUsername());
        appCompanyRules.setDeptId(SecurityUtils.getDeptId());
        if (ObjectUtils.isEmpty(appCompanyRules.getImg())) {
            String img;
            if (appCompanyRules.getType() == 1) {
                img = SYS_CONSTANT.IMG_RULES_GONG_HUI;
            } else if (appCompanyRules.getType() == 2) {
                img = SYS_CONSTANT.IMG_RULES_LAO_DONG;
            } else if (appCompanyRules.getType() == 3) {
                img = SYS_CONSTANT.IMG_RULES_SHE_HUI;
            } else if (appCompanyRules.getType() == 4) {
                img = SYS_CONSTANT.IMG_RULES_XIAN_FA;
            } else {
                img = SYS_CONSTANT.IMG_RULES_QI_TA;
            }
            appCompanyRules.setImg(img);
        }
        return appCompanyRulesMapper.insertAppCompanyRules(appCompanyRules);
    }

    /**
     * 修改规章制度
     *
     * @param appCompanyRules 规章制度
     * @return 结果
     */
    @Override
    public int updateAppCompanyRules(AppCompanyRules appCompanyRules) {
        AppCompanyRules rules = this.selectAppCompanyRulesById(appCompanyRules.getId());
        if (ObjectUtils.isEmpty(rules)) throw new RuntimeException("对象不存在");
        if (0 != rules.getDelFlag()) throw new RuntimeException("对象已删除");
        appCompanyRules.setUpdateTime(DateUtils.getNowDate());
        appCompanyRules.setUpdateBy(SecurityUtils.getUsername());
        if (ObjectUtils.isEmpty(appCompanyRules.getImg())) {
            String img;
            if (appCompanyRules.getType() == 1) {
                img = SYS_CONSTANT.IMG_RULES_GONG_HUI;
            } else if (appCompanyRules.getType() == 2) {
                img = SYS_CONSTANT.IMG_RULES_LAO_DONG;
            } else if (appCompanyRules.getType() == 3) {
                img = SYS_CONSTANT.IMG_RULES_SHE_HUI;
            } else if (appCompanyRules.getType() == 4) {
                img = SYS_CONSTANT.IMG_RULES_XIAN_FA;
            } else {
                img = SYS_CONSTANT.IMG_RULES_QI_TA;
            }
            appCompanyRules.setImg(img);
        }
        return appCompanyRulesMapper.updateAppCompanyRules(appCompanyRules);
    }

    /**
     * @param id     id
     * @param status 状态
     * @return 结果
     */
    @Override
    public int changeStatus(Long id, Integer status) {
        AppCompanyRules rules = this.selectAppCompanyRulesById(id);
        if (ObjectUtils.isEmpty(rules)) throw new RuntimeException("对象不存在");
        if (0 != rules.getDelFlag()) throw new RuntimeException("对象已删除");
        AppCompanyRules appCompanyRules = new AppCompanyRules();
        appCompanyRules.setId(id);
        appCompanyRules.setStatus(status);
        appCompanyRules.setUpdateTime(DateUtils.getNowDate());
        appCompanyRules.setUpdateBy(SecurityUtils.getUsername());
        return appCompanyRulesMapper.updateAppCompanyRules(appCompanyRules);
    }

    /**
     * 批量删除规章制度
     *
     * @param ids 需要删除的规章制度主键
     * @return 结果
     */
    @Override
    public int deleteAppCompanyRulesByIds(Long[] ids) {
        return appCompanyRulesMapper.deleteAppCompanyRulesByIds(ids);
    }

    /**
     * 删除规章制度信息
     *
     * @param id 规章制度主键
     * @return 结果
     */
    @Override
    public int deleteAppCompanyRulesById(Long id) {
        return appCompanyRulesMapper.deleteAppCompanyRulesById(id);
    }
}
